﻿using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Realize;
using System.Threading.Tasks;
using WebApplication9.Controllers.ClientSet;

namespace WebApplication9.Controllers.Comp_Stand_m
{
    public class SS_InsertController : Controller
    {
        private readonly ILogger<SS_InsertController> _logger;
        private readonly IConfiguration configuration;

        public SS_InsertController(ILogger<SS_InsertController> logger, IConfiguration configuration)
        {
            this._logger = logger;
            this.configuration = configuration;
        }
        public async Task<IActionResult> Index()
        {
            ISSD ssd = new SSD();
            List<Salary_Standard_Details> list = await ssd.Server(configuration["SqlServer"]);
            ViewBag.zhi = list;
            IUsers users = new Users();
            List<User> list2 = await users.Server(configuration["SqlServer"]);
            ViewBag.zhi2 = list2;
            return View();
        }
        public async Task<IActionResult> Index2()
        {
            IGM gm = new GM();
            List<Group_Menu> list = await gm.Server(configuration["SqlServer"]);
            return Ok(new 
            {
                zhi = list
            });
        }
        public class ExtraItem
        {
            public int id { get; set; }
            public string name { get; set; }
            public decimal value { get; set; }
        }
        public async Task<ActionResult> insert(string Dig,string XC_name,string ZE,string ZDR,string DJR,DateTime DJSJ,string BZ, string extraItems)
        {
            try
            {
                // 解析JSON字符串为对象列表
                List<ExtraItem> items = JsonConvert.DeserializeObject<List<ExtraItem>>(extraItems);
                ISSD ssd = new SSD();
                List<int> list = new List<int>();
                ISS ss = new SS();
                int i = await ss.Insert(Dig, XC_name, double.Parse(ZE), ZDR, DJR, DJSJ, BZ, configuration["SqlServer"]);
                if (i>0)
                {
                    list.Add(i);
                    List<Salary_Standard> list2 = await ss.Server2(Dig,configuration["SqlServer"]);
                    Console.WriteLine("接收到的extraItems:");
                    foreach (var item in items)
                    {
                        int i2 = await ssd.Insert(Dig, XC_name, list2[0].ssd_id,item.name, (double)item.value, configuration["SqlServer"]);
                        if (i2 > 0) 
                        {
                            list.Add(i2);
                        }
                        Console.WriteLine($"ID: {item.id}, Name: {item.name}, Value: {item.value}");
                    }
                    
                    
                }
                int i3 = 0;
                for (int p=0;p<list.Count;p++) 
                {
                    if (list[p] > 0) 
                    {
                        i3++;
                    }
                }
                if (i3 == list.Count) 
                {
                    return Ok(new
                    {
                        fh = 1
                    });
                }
                return Ok(new
                {
                    fh = 0
                });
                // 在控制台输出接收到的数据（实际项目中可使用日志记录）

            }
            catch (Exception ex)
            {
                Console.WriteLine($"解析extraItems出错: {ex.Message}");
                return BadRequest(new { fh = 0 }); // 操作失败
            }
           
            return Ok();
        }
    }
}
